﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SlimDX;

namespace Gype.UI
{
    [Flags]
    public enum FontFlags : uint
    {
        NoClip = 1,
        WordWrap = 2,
        LetterWrap = 4,
        BrushPerChar = 8,
        StretchToWidth = 16,
        StretchToHeight = 32,
    }

    public class TextElement
    {
        public TextElement(SpriteFont font)
        {
            mFont = font;
            Alignment = TextAlignement.Center;
            ParagraphAlignement = ParagraphAlignement.Center;
            Color = Brush.SolidColorBrush(font.Target, System.Drawing.Color.Beige);
            Flags = FontFlags.StretchToHeight;
            Target = font.Target;
            Height = -1;
        }

        public TextElement(Rendertarget target, string fontName, int size)
        {
            mFont = target.CreateFont(fontName, size);
            Alignment = TextAlignement.Center;
            ParagraphAlignement = ParagraphAlignement.Center;
            Color = Brush.SolidColorBrush(mFont.Target, System.Drawing.Color.Beige);
            Flags = FontFlags.StretchToHeight;
            Target = target;
            Height = -1;
        }

        public void DrawText(string text, Vector2 position, Vector2 size)
        {
            mFont.DrawText(text, position, size, this);
        }

        public System.Drawing.SizeF MeasureString(string text)
        {
            return mFont.MeasureString(text);
        }

        public System.Drawing.SizeF MeasureString(string text, Vector2 maxSize)
        {
            return mFont.MeasureString(text, maxSize);
        }

        public void SetMultiLine(float lineHeight)
        {
            Height = lineHeight;
            Flags |= FontFlags.LetterWrap;
            Flags &= ~FontFlags.StretchToHeight;
            Flags &= ~FontFlags.StretchToWidth;
        }

        public void SetSingleLine()
        {
            Height = -1;
            Flags |= FontFlags.StretchToHeight;
            Flags &= ~FontFlags.LetterWrap;
        }

        /// <summary>
        /// Gets of sets the height that is used if the flags are not set
        /// to StretchToHeight. Use this property for multiline text.
        /// A negative value indicates that the size of the SpriteFont should
        /// be used.
        /// </summary>
        public float Height { get; set; }
        public TextAlignement Alignment { get; set; }
        public ParagraphAlignement ParagraphAlignement { get; set; }
        public Brush Color { get; set; }
        public FontFlags Flags { get; set; }
        public Rendertarget Target { get; private set; }

        SpriteFont mFont;
    }
}
